package utils;

import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.util.Log;

/**
 *
 * @author fcarou
 */
public class HttpHelper
{
    private String base;
    
    public HttpHelper (String base)
    {
        this.base = base;
    }
    
    public String doGet (String path)
    {
    	return doGet(path, null, null);
    }
    
    public String doGet (String path, Header header, List<NameValuePair> params)
    {
        try
        {
        	path = base + path;
        	Log.i("LOG", "path: "+path);
        	
        	if (params != null && !params.isEmpty())
        		path = agregarParametros(path, params);
        	
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet (path);
            
            get.setHeader("Accept", "application/json");
            
            if (header != null)
                get.setHeader(header);
                        
            HttpResponse res = client.execute(get);
            HttpEntity entity = res.getEntity();
            
            if (entity != null)
                return EntityUtils.toString(entity);
        }
        catch (Exception e)
        {
            Log.i("LOG", "Excepcion: " + e.getMessage() + "/" + e.getClass().getCanonicalName());
            e.printStackTrace();
        }
        
        return null;
    }
    
    public String doPost (String path, Header header, String parametro)
    {
        try
        {        	
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(base + path);

            if (header != null)
                post.setHeader(header);
            
            if (parametro != null)
            {
	            StringEntity entity = new StringEntity(parametro);
	            post.setEntity(entity);
	            post.setHeader("Content-type", "application/json");
            }
            
            post.setHeader("Accept", "application/json");
            
            HttpResponse res = client.execute(post);
            HttpEntity resEntity = res.getEntity();
            
            return EntityUtils.toString(resEntity);
        }
        catch (HttpHostConnectException ex)
        {
        	Log.e("LOG", "HttpHelper - doPost: " + ex.getMessage() + " / " + ex.getClass().getName());
        }
        catch (Exception e)
        {
        	Log.e("LOG", "HttpHelper - doPost: " + e.getMessage() + " / " + e.getClass().getName());
        }
        
        return null;
    }
    
    private String agregarParametros (String path, List<NameValuePair> params)
    {
    	String res = path + "?";
    	
    	for (int i = 0; i < params.size(); i++)
    	{
    		if (i != 0)
    			res += "&";
    		
    		res += params.get(i).getName() + "=" + params.get(i).getValue();
    	}
    	
    	return res;
    }
}